package com.xk._03真题骗._02链表;

import com.xk._03真题骗._00bean.ListNode;

/*
 * @description: https://leetcode.cn/problems/intersection-of-two-linked-lists/
 * @author: xu
 * @date: 2022/10/27 20:19
 */
public class _160相交链表 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        ListNode curHeadA = headA, curHeadB = headB;
        while (curHeadA != curHeadB) {
            curHeadA = (curHeadA == null) ? headB : curHeadA.next;
            curHeadB = (curHeadB == null) ? headA : curHeadB.next;

            // 这段代码在两个链表不相交的时候会死循环
            // curHeadA = (curHeadA.next == null) ? headB : curHeadA.next;
            // curHeadB = (curHeadB.next == null) ? headA : curHeadB.next;
        }
        return curHeadA;
    }
}
